using System;
using System.Configuration;
using System.Data;
using digit.Core;

namespace SMS.MediaManager
{
	public class CategoryManager
	{
		#region CONSTRUCTORS
		static CategoryManager()
		{
			_data = new CategoryData(ConfigurationManager.ConnectionStrings["SMS"].ConnectionString);
			_error = "";
		}
		#endregion
		
		#region ATTRIBUTES
		private static CategoryData _data;
		private static string _error;
		#endregion
		
		#region PROPERTIES
		public static string ErrorMessage
		{
			get
			{
				return _error;
			}
		}
		
		#endregion
		
		#region METHODS
        public static DataTable Insert(int categoryID, string categoryName, int parentId, string description, int typeID, string location, DateTime createdDate, string createdBy, byte status)
		{
			_error = "";
			try
			{
				object[] values = new object[9];
				values[0] = categoryID;
				values[1] = Util.ReplaceNullTo(categoryName, DBNull.Value);
                values[2] = Util.ReplaceNullTo(parentId, DBNull.Value);
				values[3] = Util.ReplaceNullTo(description, DBNull.Value);
				values[4] = Util.ReplaceNullTo(typeID, DBNull.Value);
				values[5] = Util.ReplaceNullTo(location, DBNull.Value);
				values[6] = Util.ReplaceNullTo(createdDate, DBNull.Value);
				values[7] = Util.ReplaceNullTo(createdBy, DBNull.Value);
				values[8] = Util.ReplaceNullTo(status, DBNull.Value);
		
				return _data.Insert(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}

        public static DataTable Insert(CategoryInfo info)
		{
			_error = "";
			try
			{
				object[] values = new object[9];
				values[0] = info.CategoryID;
				values[1] = Util.ReplaceNullTo(info.CategoryName, DBNull.Value);
                values[2] = Util.ReplaceNullTo(info.ParentID, DBNull.Value);
				values[3] = Util.ReplaceNullTo(info.Description, DBNull.Value);
				values[4] = Util.ReplaceNullTo(info.TypeID, DBNull.Value);
				values[5] = Util.ReplaceNullTo(info.Location, DBNull.Value);
				values[6] = Util.ReplaceNullTo(info.CreatedDate, DBNull.Value);
				values[7] = Util.ReplaceNullTo(info.CreatedBy, DBNull.Value);
				values[8] = Util.ReplaceNullTo(info.Status, DBNull.Value);
		
				return _data.Insert(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static bool Update( int categoryID,  string categoryName,  int parentID,  string description,  int typeID,  string location, byte status)
		{
			_error = "";
			try
			{
				object[] values = new object[7];
				values[0] = categoryID;
				values[1] = Util.ReplaceNullTo(categoryName, DBNull.Value);
				values[2] = parentID;
				values[3] = Util.ReplaceNullTo(description, DBNull.Value);
				values[4] = Util.ReplaceNullTo(typeID, DBNull.Value);
				values[5] = Util.ReplaceNullTo(location, DBNull.Value);
				values[6] = Util.ReplaceNullTo(status, DBNull.Value);
		
				return _data.Update(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Update( CategoryInfo info)
		{
			_error = "";
			try
			{
				object[] values = new object[7];
				values[0] = info.CategoryID;
				values[1] = Util.ReplaceNullTo(info.CategoryName, DBNull.Value);
				values[2] = info.ParentID;
				values[3] = Util.ReplaceNullTo(info.Description, DBNull.Value);
				values[4] = Util.ReplaceNullTo(info.TypeID, DBNull.Value);
				values[5] = Util.ReplaceNullTo(info.Location, DBNull.Value);
				values[6] = Util.ReplaceNullTo(info.Status, DBNull.Value);
		
				return _data.Update(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Delete( int categoryID)
		{
			_error = "";
			try
			{
				object[] values = { categoryID };
				return _data.Delete(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Delete( CategoryInfo info)
		{
			_error = "";
			try
			{
				object[] values = { info.CategoryID };
				return _data.Delete(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static CategoryInfo Select( int categoryID)
		{
			_error = "";
			try
			{
				object[] values = { categoryID };
				DataTable dTable = _data.Select(values);
				if (dTable.Rows.Count < 1) return null;
		
				
				CategoryInfo info = new CategoryInfo(dTable.Rows[0]);
				return info;
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}

        public static DataTable SelectByType(int intType)
        {
            _error = "";
            try
            {
                object[] values = { intType };
                return _data.SelectByType(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }
		
		public static DataTable SelectAll()
		{
			_error = "";
			try
			{
				return _data.SelectAll();
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable Search( string whereClause,  string sortOrder,  int pageIndex,  int pageSize)
		{
			_error = "";
			try
			{
				object[] values = { whereClause, sortOrder };
				int index = (pageIndex * pageSize) - 1;
				if (index < 0) index = 0;
				return _data.Search(values, index, pageSize);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable Search( string whereClause,  string sortOrder)
		{
			return CategoryManager.Search(whereClause, sortOrder, 0, 0);
		}
		
		public static DataTable Search( int pageIndex,  int pageSize)
		{
			return CategoryManager.Search( "", "", pageIndex, pageSize);
		}
		
		#endregion
		
	}
}